/*
 * @Descripttion: 
 * @version: 1.0.0
 * @Author: 李中华
 * @Date: 2024-10-17 15:14:55
 * @LastEditTime: 2024-10-18 09:53:21
 */
const express = require('express');
const path = require('path');

const app = express();
const PORT = 3456;

// 定义静态文件目录
app.use(express.static(path.join(__dirname, 'public')));

// 路由设置，部署前端静态页面
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'public', 'index.html'));
});



app.get('/api/data', (req, res) => {
     // 允许所有源发起跨域请求
    //   res.setHeader('Access-Control-Allow-Origin','*')
    //   res.setHeader('Access-Control-Allow-Origin','http://localhost:3456')// 只能指定一个


    // 模拟数据
    const data = {
        name: 'John Doe',
        age: 30,
        city: 'New York'
    };

    res.json(data);
});

// 启动服务器
app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});
